﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using WpfControlDemo.Models;
using WpfControlDemo.ViewModels.Base;

namespace WpfControlDemo.ViewModels.控件.输入控件
{
    public class ComboBoxWindowViewModel : INotifyPropertyChanged
    {
        public ObservableCollection<ClassModel> ClassModels { get; set; }

        public ObservableCollection<string> PJIDList { get; set; } = new ObservableCollection<string>();

        public ComboBoxWindowViewModel()
        {
            ClassModels = GetClassModel();
            SelectedValue = 1;
            foreach (var model in ClassModels)
            {
                PJIDList.Add(model.ClassName);
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// 此处模拟了数据库或者接口查询
        /// </summary>
        /// <returns></returns>
        public ObservableCollection<ClassModel> GetClassModel()
        {
            ObservableCollection<ClassModel> students = new ObservableCollection<ClassModel>();
            for (int i = 1; i <= 5; i++)
            {
                ClassModel studentModel = new ClassModel
                {
                    ClassId = i,
                    ClassName = $"{i}班"
                };
                students.Add(studentModel);
            }
            return students;
        }

        private int _selectedValue;

        /// <summary>
        /// 用于设置选中项以及获取选中项
        /// </summary>
        public int SelectedValue
        {
            get { return _selectedValue; }
            set
            {
                _selectedValue = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedValue)));
            }
        }

        private ClassModel _selectedItem;
        /// <summary>
        /// 用于获取ComBox选中项
        /// </summary>
        public ClassModel SelectedItem
        {
            get { return _selectedItem; }
            set { _selectedItem = value; }
        }


        /// <summary>
        /// 下拉列表选择事件
        /// </summary>
        /// <param name="sender">出发这个事件的对象</param>
        /// <param name="e">事件带过来的参数，为SelectionChanged提供数据</param>
        public void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // 这里没有使用sender获取选中项数据
            var item = (sender as System.Windows.Controls.ComboBox).SelectedItem as ClassModel;
            string name = item.ClassName;
        }

        public CommandBase ButtonSelectedValueCommand
        {
            get => new CommandBase
            {
                DoExecute = new Action<object>(obj =>
                {
                    string value = SelectedValue.ToString();
                })
            };
        }

        private string selectedText;

        public string SelectedText
        {
            get { return selectedText; }
            set
            {
                selectedText = value;
            }
        }

    }
}
